﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sisocana.Core;
using Sisocana.Core.Interfaces;
using Sisocana.Core.Models;
using Sisocana.Seguridad;

namespace Sisocana.Controllers
{
    public class NoticiasController : Controller
    {
        private IFuenteService _fuenteService;

        public NoticiasController()
            :this(new FuenteService())
        {
        }

        public NoticiasController(IFuenteService fuenteService)
        {
            _fuenteService = fuenteService;
        }

        //
        // GET: /News/
        [Authorize]
        public ActionResult Index()
        {
            return View(_fuenteService.GetAllNoticia());
        }

        //
        // GET: /News/Details/5
        [Authorize]
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /News/Create
        [Authorize]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /News/Create
        [Authorize]
        [HttpPost]
        public ActionResult Create([Bind(Exclude = "logo")] NoticiaModel noticia)
        {
          try
          {
                noticia.obtenerInformacion();
                int result = _fuenteService.addNoticia(noticia);
                if (result == 0)
                    return RedirectToAction("Index");
                else
                {
                    ModelState.AddModelError("", "An error ocurred and the source was not added.");
                    return View(noticia);
                }
          }
          catch (Exception e)
          {
              ModelState.AddModelError("", e);
              return View(noticia);
          }
        }
        
        //
        // GET: /News/Edit/5
        [AdminGralAuthorize]
        public ActionResult Edit(string id)
        {
            NoticiaModel rs = _fuenteService.getSigleNoticia(a => a.nombre.Trim() == id);
            rs.feedurl = rs.feedurl.Trim();
            return View(rs);
        }

        //
        // POST: /News/Edit/5
        [AdminGralAuthorize]
        [HttpPost]
        public ActionResult Edit(string id, NoticiaModel nm)
        {

          try
          {
                nm.obtenerInformacion();
                int result = _fuenteService.updateNoticia(nm);
                if (result == 0)
                    return RedirectToAction("Index");
                else
                {
                    ModelState.AddModelError("", "An error ocurred and the source was not added.");
                    return View(nm);
                }
          }
          catch (Exception e)
          {
              ModelState.AddModelError("", e);
              return View(nm);
          }
        }

        //
        // GET: /News/Delete/5
        [AdminGralAuthorize]
        public ActionResult Delete(string id)
        {
            NoticiaModel n = _fuenteService.getSigleNoticia(s => s.nombre.Trim() == id.Trim());
            return View(n);
        }

        //
        // POST: /News/Delete/5
        [AdminGralAuthorize]
        [HttpPost]
        public ActionResult Delete([Bind(Exclude = "logo")]NoticiaModel noti)
        {
            try
            {
                int res = _fuenteService.deleteNoticia(noti);
                if (res == 0)
                    return RedirectToAction("Index");
                else
                {
                    return RedirectToAction("MessageID","Error",new{ errorid=res} );
                }
            }
            catch
            {
                return RedirectToAction("MessageID", "Error", new { errorid = -110 });
            }
        }
    }
}
